export default class AngleUtils {
	// 角度转弧度
	public static angleToRadian(angle: number) {
		return (Math.PI / 180) * angle;
	}
	// 弧度转角度
	public static radianToAngle(radian: number) {
		return (radian * 180) / Math.PI;
	}

	//获取两坐标的夹角(弧度)
	public static getAngle(p1: cc.Vec3, p2: cc.Vec3) {
		let p3 = p2.sub(p1);
		let r = Math.atan2(p3.y, p3.x);
		return r;
	}

	public static angle(p1:cc.Vec2,p2:cc.Vec2){
		let p=p2.sub(p1);
		return p.angle(cc.v2(1,0));
	}

	public static conventToVec2(radian: number, speed: number) {
		let v = cc.v2();
		v.x = Math.cos(radian) * speed;
		v.y = Math.sin(radian) * speed;
		return v;
	}

	//是否相交
	public static isCross(a: cc.Vec2, b: cc.Vec2, c: cc.Vec2, d: cc.Vec2) {
		let denominator = (b.y - a.y) * (d.x - c.x) - (a.x - b.x) * (c.y - d.y);
		if (denominator == 0) {
			return false;
		}
		return true;
	}

	// 两线段的交点坐标
	public static crossPoint(a: cc.Vec2, b: cc.Vec2, c: cc.Vec2, d: cc.Vec2) {
		let denominator = (b.y - a.y) * (d.x - c.x) - (a.x - b.x) * (c.y - d.y);
		let x =
			((b.x - a.x) * (d.x - c.x) * (c.y - a.y) +
				(b.y - a.y) * (d.x - c.x) * a.x -
				(d.y - c.y) * (b.x - a.x) * c.x) /
			denominator;
		let y =
			-(
				(b.y - a.y) * (d.y - c.y) * (c.x - a.x) +
				(b.x - a.x) * (d.y - c.y) * a.y -
				(d.x - c.x) * (b.y - a.y) * c.y
			) / denominator;
		return cc.v2(x,y);
	}
}
